<?php
namespace Home\Controller;

class UserController extends BaseController {
	/**
	 * 登录后的默认页
	 */
	public function index() {
		if (IS_GET) {
			$Form = M('member');
			$result = $Form -> where(array('id' => $_SESSION['uid'])) -> find();
			$this -> assign('result', $result);
			$this -> display();
		} else if (IS_POST) {
			//退出
			if ($_POST['user'] == 'exit') {
				session_unset();
				$this -> redirect("/home/index/login");
			}
		}
	}

	/**
	 * 个人信息(info)页面
	 */
	public function info() {
		$Form = M('member');
		if (IS_GET) {
			$result = $Form -> where(array('id' => $_SESSION['uid'])) -> find();
			$this -> assign('result', $result);
			$this -> display();
		} else if (IS_POST) {
			//退出
			if ($_POST['user'] == 'exit') {
				session_unset();
				$this -> redirect("/home/index/login");
			}
			//编辑个人信息
			if ($Form -> create()) {
				$res = $Form -> where(array('id' => $_SESSION["uid"])) -> save();
				$obj = (object) array();
				$obj -> code = 0;
				$obj -> message = "修改成功";
				if ($res) {
					$this -> ajaxReturn($obj);
				}
			} else {
				$obj = (object) array();
				$obj -> code = 101;
				$obj -> message = "修改失败";
				$this -> ajaxReturn($obj);
			}
		}
	}

	/**
	 * 修改密码(updatePwd)页面
	 */
	public function updatePwd() {
		$Form = M('member');
		if (IS_GET) {
			$result = $Form -> where(array('id' => $_SESSION['uid'])) -> find();
			$this -> assign('result', $result);
			$this -> display();
		} else if (IS_POST) {
			//退出
			if ($_POST['user'] == 'exit') {
				session_unset();
				$this -> redirect("/home/index/login");
			}
			//更新密码
			$result = $Form -> where(array('id' => $_SESSION['uid'])) -> find();
			$NewPassword = md5($_POST["NewPassword"]);
			$res = $Form -> where(array('id' => $_SESSION["uid"])) -> setField('password', $NewPassword);
			if ($res) {
				$obj = (object) array();
				$obj -> code = 0;
				$obj -> message = "修改成功";
				$this -> ajaxReturn($obj);
			} else {
				$obj = (object) array();
				$obj -> code = 101;
				$obj -> message = "修改失败";
				$this -> ajaxReturn($obj);
			}
		}
	}

	/**
	 * 用户列表(items)页面
	 */
	public function items() {
		$Form = M('member');
		$result = $Form -> where(array('id' => $_SESSION['uid'])) -> find();
		$this -> assign('result', $result);
		$this -> display();
		//退出
		if ($_POST['user'] == 'exit') {
			session_unset();
			$this -> redirect("/home/index/login");
		}
	}

	/**
	 * items页面，新增用户
	 */
	public function addUserinfo() {
		if (IS_POST) {
			//头像上传
//			print_r($_FILES);
//			$filename = $_FILES["img"]["name"];
//			$type = $_FILES["img"]["type"];
//			$tmp_name = $_FILES["img"]["tmp_name"];
//			$size = $_FILES["img"]["size"];
//			$error = $_FILES["img"]["error"];
			//将服务器上的临时文件移动到指定目录下
			//移动成功返回true，否则返回false
//			move_uploaded_file($filename, "/PUBLIC/dist/img/".$filename);
			
			//member表添加用户
			$Form = D('member');
//			$Form -> img = $_FILES["img"]["name"];
			$Form -> img = "/Public/dist/img/user1-128x128.jpg";
			$Form -> id = $_POST["uid"];
			$Form -> username = $_POST["username"];
			$Form -> password = md5($_POST["password"]);
			$Form -> birthday = $_POST["birthday"];
			$Form -> sex = $_POST["sex"];
			$Form -> phone = $_POST["phone"];
			$Form -> email = $_POST["email"];
			$Form -> address = $_POST["address"];
			$result = $Form -> add();
			//group_access表添加角色
			$m = D('auth_group_access');
			$m -> uid = $_POST["uid"];
			$m -> group_id = $_POST["group_id"];
			$res = $m -> add();
			if ($result && $res) {
				$obj = (object) array();
				$obj -> code = 0;
				$obj -> message = "数据添加成功成功";
				$this -> ajaxReturn($obj);
			} else {
				$obj = (object) array();
				$obj -> code = 101;
				$obj -> message = "数据添加失败";
				$this -> ajaxReturn($obj);
			}
		}
	}

	/**
	 * 获取member表用户个数
	 */
	public function userCount() {
		$Form = M('member');
		$result = $Form -> select();
		$this -> ajaxReturn(count($result));
	}

	/**
	 * items页面，编辑
	 */
	public function updateUserinfo() {
		if (IS_POST) {
			$email = $_REQUEST["sole"];
			$Form = M('member');
			if ($Form -> create()) {
				$result = $Form -> where("email='{$email}'") -> save();
				if ($result) {
					$obj = (object) array();
					$obj -> code = 0;
					$obj -> message = "操作成功";
					$this -> ajaxReturn($obj);
				} else {
					$obj = (object) array();
					$obj -> code = 101;
					$obj -> message = "更新失败";
					$this -> ajaxReturn($obj);
				}
			} else {
				$this -> error($Form -> getError());
			}
		}
	}

	/**
	 * items页面，删除用户
	 */
	public function delUserinfo() {
		if (IS_POST) {
			$email = $_REQUEST["weiyi"];
			$Form = M('member');
			$result = $Form -> where("email='{$email}'") -> delete();
			if ($result) {
				$obj = (object) array();
				$obj -> code = 0;
				$obj -> message = "删除成功";
				$this -> ajaxReturn($obj);
			} else {
				$obj = (object) array();
				$obj -> code = 101;
				$obj -> message = "删除失败";
				$this -> ajaxReturn($obj);
			}
		}
	}

	/**
	 * 角色管理(role)页面
	 */
	public function role() {
		$Form = M('member');
		$result = $Form -> where(array('id' => $_SESSION['uid'])) -> find();
		$this -> assign('result', $result);
		$this -> display();
		//退出
		if ($_POST['user'] == 'exit') {
			session_unset();
			$this -> redirect("/home/index/login");
		}
	}

	/**
	 * role页面，新增角色
	 */
	public function addRole() {
		if (IS_POST) {
			$Form = M('auth_group');
			$ret = $Form -> where(array('title' => $_POST["title"])) -> select();
			if (!$ret) {
				$Form -> title = $_POST["title"];
				$Form -> status = $_POST["status"];
				$result = $Form -> add();
				if ($result) {
					$obj = (object) array();
					$obj -> code = 0;
					$obj -> message = "新增成功";
					$this -> ajaxReturn($obj);
				} else {
					$obj = (object) array();
					$obj -> code = 101;
					$obj -> message = "操作失败";
					$this -> ajaxReturn($obj);
				}
			} else {
				$this -> error('名称已存在！');
			}
		}
	}

	/**
	 * role页面，删除角色
	 */
	public function delRole() {
		if (IS_POST) {
//			$m = M('auth_group_access');
//			if(){
//				
//			}
//			
			
			$title = $_REQUEST["title"];
			$Form = M('auth_group');
			$result = $Form -> where("title='{$title}'") -> delete();
			if ($result) {
				$obj = (object) array();
				$obj -> code = 0;
				$obj -> message = "删除成功";
				$this -> ajaxReturn($obj);
			} else {
				$obj = (object) array();
				$obj -> code = 101;
				$obj -> message = "删除失败";
				$this -> ajaxReturn($obj);
			}
		}
	}

	/**
	 * 权限管理(auth)页面
	 */
	public function auth() {
		$Form = M('member');
		$result = $Form -> where(array('id' => $_SESSION['uid'])) -> find();
		$this -> assign('result', $result);
		$this -> display();
		//退出
		if ($_POST['user'] == 'exit') {
			session_unset();
			$this -> redirect("/home/index/login");
		}
	}

	/**
	 * items用户列表数据
	 */
	public function itemsdb() {
		$columnindex = $_GET['order'][0]['column'];
		$dir = $_GET['order'][0]['dir'];
		$start = $_GET['start'];
		$length = $_GET['length'];
		$search = $_GET['search']['value'];
		$m = M("member");
		if ($columnindex == 1) {
			$column = "username";
		} else if ($columnindex == 2) {
			$column = "sex";
		} else if ($columnindex == 3) {
			$column = "email";
		} else if ($columnindex == 4) {
			$column = "address";
		} else if ($columnindex == 5) {
			$column = "phone";
		} else if ($columnindex == 6) {
			$column = "birthday";
		} else {
			$column = "id";
		}
		$res = $m -> where("username like '%{$search}%' or email like '%{$search}%' or address like '%{$search}%' or sex like '%{$search}%' or phone like '%{$search}%' or birthday like '%{$search}%'") -> field("id,username,email,address,sex,phone,birthday") -> order($column . " " . $dir) -> limit($start, $length) -> select();
		$ret = (object) array();
		$ret -> data = array();
		$ret -> draw = $_GET['draw'];
		for ($i = 0; $i < count($res); $i++) {
			$aga = M("auth_group_access");
			$group = $aga -> field('title') -> join('think_auth_group on think_auth_group_access.group_id=think_auth_group.id') -> where('uid=' . ($res[$i]["id"]) . '') -> find();
			$ret -> data[] = array($i + 1, $res[$i]["username"], $res[$i]["sex"], $res[$i]["email"], $res[$i]["address"], $res[$i]["phone"], $res[$i]["birthday"], $group["title"], '<div class="btn-group"><button type="button" class="btn btn-default" name="modal-update" data-toggle="modal" data-target="#modal-update"  data-id="' . $res[$i]["id"] . '" >编辑</button><button type="button" class="btn btn-default"    data-toggle="modal"  data-target="#modal-del" name="modal-del" data-id="' . $res[$i]["id"] . '" >删除</button></div>');
		}
		$ret -> recordsFiltered = 10;
		$ret -> recordsTotal = 10;
		$this -> ajaxReturn($ret);
	}

	/**
	 * 角色右表所有数据
	 */
	public function roleList() {
		$columnindex = $_GET['order'][0]['column'];
		$dir = $_GET['order'][0]['dir'];
		$start = $_GET['start'];
		$length = $_GET['length'];
		$search = $_GET['search']['value'];
		$m = M("member");
		if ($columnindex == 1) {
			$column = "username";
		} else if ($columnindex == 2) {
			$column = "email";
		} else {
			$column = "id";
		}
		$res = $m -> where("username like '%{$search}%' or email like '%{$search}%' ") -> field("username,email") -> order($column . " " . $dir) -> limit($start, $length) -> select();
		$ret = (object) array();
		$ret -> data = array();
		$ret -> draw = $_GET['draw'];
		for ($i = 0; $i < count($res); $i++) {
			$ret -> data[] = array($i + 1, $res[$i]["username"], $res[$i]["email"], "<button type='button' class='btn btn-default'    data-toggle='modal'  data-target='#modal-updateRole' name='updateRolebtn' >修改角色</button>");
		}
		$ret -> recordsFiltered = 10;
		$ret -> recordsTotal = 10;
		$this -> ajaxReturn($ret);
	}

	/**
	 * 用户列表页面点击新增时加载角色
	 */
	public function loadRole() {
		$Form = M('auth_group');
		$result = $Form -> field("id,title") -> select();
		$this -> ajaxReturn($result);
	}

	/**
	 * 修改角色
	 */
	public function updateRole() {
		$uid = $_POST["uid"];
		$data['group_id'] = $_POST["group_id"];
		$Form = M("auth_group_access");
		if ($Form -> create()) {
			$result = $Form -> where(array('uid' => $uid)) -> save($data);
			if ($result) {
				$obj = (object) array();
				$obj -> code = 0;
				$obj -> message = "修改成功";
				$this -> ajaxReturn($obj);
			} else {
				$obj = (object) array();
				$obj -> code = 101;
				$obj -> message = "修改失败";
				$this -> ajaxReturn($obj);
			}
		} else {
			$this -> error($Form -> getError());
		}

	}

	/**
	 * role角色管理数据(左表)
	 */
	public function leftRole() {
		$columnindex = $_GET['order'][0]['column'];
		$dir = $_GET['order'][0]['dir'];
		$start = $_GET['start'];
		$length = $_GET['length'];
		$search = $_GET['search']['value'];
		$m = M("auth_group");
		if ($columnindex == 1) {
			$column = "title";
		} else {
			$column = "id";
		};
		$res = $m -> where("title like '%{$search}%'") -> field('id,title') -> order($column, $dir) -> limit($start, $length) -> select();
		$obj = (object) array();
		$ret -> data = array();
		$ret -> draw = $_GET['draw'];
		$ret -> search = $search;
		for ($i = 0; $i < count($res); $i++) {
			$ret -> data[] = array($i + 1, $res[$i]["title"], "可用", '<div class="btn-group"><button type="button" class="btn btn-default"    data-toggle="modal"  data-target="#modal-del" name="modal-del"  data-id="' . $res[$i]["id"] . '">删除</button></div>');
		}
		$sum = count($m -> select());
		$num = count($res);
		$ret -> recordsFiltered = $num;
		$ret -> recordsTotal = $sum;
		$this -> ajaxReturn($ret);
	}

	/**
	 * role角色管理数据(右表)
	 */
	public function rightRole() {
		$id = $_POST['uid'];
		$m = M("member");
		$res = $m -> field("id,username,email") -> join("think_auth_group_access on think_auth_group_access.uid=think_member.id ") -> where("group_id=" . $id) -> select();
		$this -> ajaxReturn($res);
	}

	/**
	 * auth权限管理数据(左表)
	 */
	public function leftAuth() {
		$columnindex = $_GET['order'][0]['column'];
		$dir = $_GET['order'][0]['dir'];
		$start = $_GET['start'];
		$length = $_GET['length'];
		$search = $_GET['search']['value'];
		$m = M("auth_group");
		if ($columnindex == 1) {
			$column = "title";
		} else {
			$column = "id";
		};
		$res = $m -> where("title like '%{$search}%'") -> field('id,title,rules') -> order($column, $dir) -> limit($start, $length) -> select();
		$obj = (object) array();
		$ret -> data = array();
		$ret -> draw = $_GET['draw'];
		$ret -> search = $search;
		for ($i = 0; $i < count($res); $i++) {
			$ret -> data[] = array($i + 1, $res[$i]["title"], '可用', '<div class="btn-group"><button type="button" class="btn btn-default"    data-toggle="modal"  data-target="#modal-editAuth" name="modal-editAuth"  data-id="' . $res[$i]["id"] . '">编辑权限</button></div>');
		}
		$sum = count($m -> select());
		$num = count($res);
		$ret -> recordsFiltered = $num;
		$ret -> recordsTotal = $sum;
		$this -> ajaxReturn($ret);
	}

	/**
	 * auth权限管理数据(右表)
	 */
	public function rightAuth() {
		$columnindex = $_GET['order'][0]['column'];
		$dir = $_GET['order'][0]['dir'];
		$start = $_GET['start'];
		$length = $_GET['length'];
		$search = $_GET['search']['value'];
		$m = M("auth_rule");
		if ($columnindex == 1) {
			$column = "name";
		} else if ($columnindex == 2) {
			$column = "title";
		} else {
			$column = "id";
		}
		;
		$res = $m -> where("name like '%{$search}%' or title like '%{$search}%'") -> field('id,name,title') -> order($column, $dir) -> limit($start, $length) -> select();
		$obj = (object) array();
		$ret -> data = array();
		$ret -> draw = $_GET['draw'];
		$ret -> search = $search;
		for ($i = 0; $i < count($res); $i++) {
			$ret -> data[] = array($i + 1, $res[$i]["name"], $res[$i]["title"]);
		}
		$sum = count($m -> select());
		$num = count($res);
		$ret -> recordsFiltered = $num;
		$ret -> recordsTotal = $sum;
		$this -> ajaxReturn($ret);
	}

	/**
	 * auth右表添加权限 addAuthForm
	 */
	public function addAuth() {
		$Form = D('auth_rule');
		if ($Form -> create()) {
			$result = $Form -> add();
			if ($result) {
				$obj = (object) array();
				$obj -> code = 0;
				$obj -> message = "数据添加成功";
				$this -> ajaxReturn($obj);
			} else {
				$obj = (object) array();
				$obj -> code = 101;
				$obj -> message = "数据添加失败";
				$this -> ajaxReturn($obj);
			}
		} else {
			$this -> error($Form -> getError());
		}
	}

	/**
	 * 获取权限
	 */
	public function getAuth() {
		$id = $_POST['uid'];
		$access = M("auth_group");
		$group = $access -> field("rules") -> where("id=" . $id) -> find();
		$rule = M("auth_rule");
		$userRules = $rule -> where('id in (' . $group['rules'] . ')') -> select();
		$this -> ajaxReturn($userRules);
	}

	/**
	 * 删除权限
	 */
	public function Rules() {
		$id = $_POST['id'];
		$rules = $_POST['rules'];
		//implode将数组转成字符串用','连接
		$str['rules'] = implode(',', $rules);
		$ag = M("auth_group");
		if ($ag -> create()) {
			$result = $ag -> where(array('id' => $id)) -> save($str);
			if ($result) {
				$obj = (object) array();
				$obj -> code = 0;
				$obj -> message = "删除成功";
				$this -> ajaxReturn($obj);
			} else {
				$obj = (object) array();
				$obj -> code = 101;
				$obj -> message = "删除失败";
				$this -> ajaxReturn($obj);
			}
		} else {
			$this -> error($ag -> getError());
		}
	}

}
?>